package com.gac.keepalive

import android.app.ActivityManager
import android.content.Context
import android.content.Context.ACTIVITY_SERVICE
import android.content.Intent
import androidx.fragment.app.FragmentActivity

/**
 * FileName: KeepAliveHelper
 * Author: gacmy
 * Date: 2021/10/9 2:17 PM
 * Description:
 */
class KeepAliveHelper {
    companion object{
        private fun checkService(context: Context?):Boolean{
            context?.apply {
                var manager = getSystemService(ACTIVITY_SERVICE) as ActivityManager
                manager.getRunningServices(Integer.MAX_VALUE).forEach {
                    if ("com.gac.keepalive.ForegroundCoreService" == it.service.className) {
                        return true
                    }
                }
            }
            return false
        }

        //启动前台进程服务
        fun startService(context: Context){
            if(!checkService(context)){
                var service = Intent(context,ForegroundCoreService::class.java)
                context.startService(service)
            }
        }
        //打开白名单
        fun openWhiteList(context: Context?){
            context?.apply {
                openWhiteListDialog(this)
            }
        }
        //请求优化电池
        fun requestOptimizeBattery(fragmentActivity: FragmentActivity?,listener: IPermissionListener){
            fragmentActivity?.apply {
                OptimizeBatteryHelper.init(this).requestBattery(listener)
            }
        }
    }


}